import mplfinance as mpf
import matplotlib.pyplot as plt
import pandas as pd
import datetime

import tushare as ts
ts.set_token("6667cd4a2326f2f937062a0f4fb59aea5c56d13b1f6f26225f115fe9")
pro = ts.pro_api()

ts_code="002108.sz"
def mpfplot(ts_code,days=14):
    today = datetime.date.today()
    start_day=(today-datetime.timedelta(days)).strftime('%Y%m%d') #9-2个交易日数据拟合
    df = pro.daily(ts_code=ts_code, fields='trade_date,open,high,low,close,vol',start_date=start_day)
    df=df.rename(columns={'vol':'volume'})
    df=df.iloc[::-1]
    df['trade_date'] = pd.to_datetime(df['trade_date'],format = '%Y-%m-%d')
    df.set_index(['trade_date'],inplace = True) #把1,2,3拿掉,直接用trade_date做行号
    print (df)
    my_color = mpf.make_marketcolors(up='red',down='green',edge='inherit',volume='inherit',inherit=True) #上涨用红色,下跌用绿色,框线颜色和内部一致,成交量同样
    my_style = mpf.make_mpf_style(gridaxis='both',gridstyle ='-.',y_on_right=True,marketcolors=my_color)
    mpf.plot(df,type="candle",style=my_style,volume=True,figratio=(2,1),figscale=1,savefig="T://VS_CODE//TEST//%s.png"%ts_code)

mpfplot(ts_code,days=30)